﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.Slaughters;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.Slaughters
{
    /// <summary>
    /// 添加屠宰企业信息命令处理
    /// </summary>
    public class AddSlaughterCorporateCommandHandlter : IRequestHandler<AddSlaughterCorporateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 屠宰企业仓储
        /// </summary>
        private readonly IBaseRepository<SlaughterCorporateModel> slaughterCorporateRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="slaughterCorporateRep">屠宰企业仓储</param>
        /// <param name="mapper">映射</param>
        public AddSlaughterCorporateCommandHandlter(IBaseRepository<SlaughterCorporateModel> slaughterCorporateRep, IMapper mapper)
        {
            this.slaughterCorporateRep = slaughterCorporateRep;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(AddSlaughterCorporateCommand request, CancellationToken cancellationToken)
        {

            //添加屠宰企业信息
            var slaughterCorporate = mapper.Map<SlaughterCorporateModel>(request);
            slaughterCorporate.Creatime = DateTime.Now;
            await slaughterCorporateRep.AddAsync(slaughterCorporate);
            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "添加屠宰企业成功"
            };
        }
    }
}
